#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

typedef struct Node{
    int key;
    int value;
    struct Node *next;
} Node;

Node *createNode(int key,int value){
    Node *node = (Node*)malloc(sizeof(Node));
    node->key = key;
    node->value = value;
    node->next = NULL;
    return node;
}

int hash(int key){
    return key % SIZE;
}

void insert(Node **arr,int key,int value){
    int index = hash(key);
    Node *head = arr[index];
    if(head==NULL){
        arr[index] = createNode(key,value);
        return;
    }
    while (head->next != NULL){
        if (head->key == key) {
            // 如果存在，更新值并返回
            head->value = value;
            printf("Updated element %d in the hashtable.\n", value);
            return;
        }
        head = head->next;
    }
    head->next = createNode(key,value);
    printf("Successfully inserted element %d into the hashtable.\n",value);
}

int search(Node **arr,int key){
    int index = hash(key);
    Node *head = arr[index];
    if(head==NULL){
        return -1;
    }
    while (head->key != key){
        head = head->next;
    }
    return head->value;
}


int main(){
    Node *hashtable[SIZE];
    for(int i=0;i<SIZE;i++){hashtable[i] = NULL;} // 初始化哈希表
    insert(hashtable,21,210);
    insert(hashtable,13,130);
    insert(hashtable,400,4000);
    insert(hashtable,33,330);
    insert(hashtable,913,9130);
    int key = 33;
    int value = search(hashtable,key);
    printf("%d: %d\n",key,value);
    return 0;
}